//
//  DDBaseViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/3/13.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDBaseViewController.h"
#import "AFNetworking.h"
#import "SVProgressHUD.h"
#import "Reachability.h"


@interface DDBaseViewController ()

@property (strong, nonatomic) Reachability *googleReach;

@end

@implementation DDBaseViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
     
    [self addCustomView];
    [self setCustomView];
    [self addReachability];
    
    
    self.navigationController.navigationBar.translucent = false;
    self.navigationController.navigationBar.barTintColor = COLOR_NAVIGATIONBAR;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self hideLoading];
}

- (void)addRightButtonItemWithImage:(UIImage *)image action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 28, 28);
    
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = barButtonItem;
}

- (void)addLeftButtonItemWithImage:(UIImage *)image action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 28, 28);
    
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = barButtonItem;
}

-(UIBarButtonItem *)customBackItemWithTarget:(id)target action:(SEL)action {
    
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navBack.png"] style:UIBarButtonItemStyleDone target:self action:@selector(doBack)];
    
    return  btn;
    
}


-(void)doBack {
   
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark
#pragma mark - Reachability

- (void)addReachability
{
//    self.googleReach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
//    
//    [self.googleReach startNotifier];
}

- (BOOL)networkIsReachable
{
    return [self.googleReach isReachable];
}

-(void)reachabilityChanged:(NSNotification*)note
{
    Reachability * reach = [note object];
    
    if(reach == self.googleReach)
    {
        if([reach isReachable]) {
            
        }
        else {
            [self showText:STR_NETWORK_ERROR];
        }
    }
}

#pragma mark
#pragma mark - Public Method

- (void)addCustomView
{
    
}

- (void)setCustomView
{
    
}


- (BOOL)isValidateResponseData:(id)responseObject
{
    BOOL success = NO;
    if (responseObject && responseObject[@"code"]) {
        success = [responseObject[@"code"] integerValue] == 1 ? YES : NO;
    }
    
    return success;
}


- (void)responseUnValidate:(id)responseObject
{
    WEAKSELF
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf showText:[NSString stringWithFormat:@"%@",responseObject[@"msg"]]];
    });
}


- (void)requestFaild
{
    [self hideLoading];
    WEAKSELF
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (![weakSelf networkIsReachable]) {
            [weakSelf showText:STR_NETWORK_ERROR];
        } else {
            [weakSelf showErrorWithText:STR_SERVICE_ERROR];
        }
    });
}

#pragma mark -
#pragma mark - ProgressHUD

- (void)showToast:(NSString *)text
{
    [[LDShowHUB shareInstance] showToastNoti:text];
//    [self showToast:text duration:1.5 position:nil];
}

- (void)showToastOnCenter:(NSString *)text
{
    [[LDShowHUB shareInstance] showToastNoti:text];
    
//    [self showToast:text duration:1.5 position:CSToastPositionCenter];
}

- (void)showToast:(NSString *)text duration:(NSTimeInterval)time position:(CGPoint)position
{
//    [[LDShowHUB shareInstance] showToastNoti:text];
    [self.view makeToast:text duration:1.5 position:position];
//    [self.view makeToast:text duration:time position:position];
}


- (void)showText:(NSString *)text
{
    [self showToastOnCenter:text];
//    [self showText:text onView:self.view];
}
- (void)showText:(NSString *)text onView:(UIView *)view
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setViewForExtension:view];
    [SVProgressHUD showInfoWithStatus:text];
}

- (void)showSuccess
{
    [self showSuccessWithText:@""];
}
- (void)showSuccessWithText:(NSString *)successText
{
    [SVProgressHUD setMinimumDismissTimeInterval:1.5];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showSuccessWithStatus:successText];
}

- (void)showErrorWithText:(NSString *)errorText
{
    [self showErrorWithText:errorText onView:self.view];
}
- (void)showErrorWithText:(NSString *)errorText onView:(UIView *)view
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setViewForExtension:view];
    [SVProgressHUD showErrorWithStatus:errorText];
}

- (void)showLoading
{
    [self showLoadingWithText:@"Loading..."];
}

- (void)showLoadingWithText:(NSString *)text
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    
    [SVProgressHUD showWithStatus:text];
}

- (void)showLoadingWithText:(NSString *)text onView:(UIView *)view
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setViewForExtension:view];
    [SVProgressHUD showWithStatus:text];
}

- (void)hideLoading
{
    [SVProgressHUD dismiss];
}


#pragma mark - navigation items
- (UIBarButtonItem *)confirmItem {
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [btn setImage:[UIImage imageNamed:@"common_nav_ok"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(actionConfirm:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    return item;
}

- (void)actionConfirm:(UIButton *)btn {
    
}

- (UIBarButtonItem *)cancelItem {
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [btn setImage:[UIImage imageNamed:@"common_nav_cancel"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(actionCancel:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    return item;
}

- (void)actionCancel:(UIButton *)btn {
    
}

@end
